{% extends 'base.html' %}
{% load static %}

{% block title %}盘点计划详情 - {{ plan.name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <!-- 计划基本信息 -->
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-clipboard-list me-2"></i>{{ plan.name }}</h5>
                    <div>
                        {% if plan.status == 'draft' and can_edit %}
                            <a href="{% url 'assets:inventory_plan_start' plan.pk %}" class="btn btn-success btn-sm">
                                <i class="fas fa-play me-1"></i>启动盘点
                            </a>
                        {% endif %}
                        <a href="{% url 'assets:inventory_plan_list' %}" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left me-1"></i>返回列表
                        </a>
                    </div>
                </div>
                
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>盘点类型：</strong></td>
                                    <td><span class="badge bg-info">{{ plan.get_plan_type_display }}</span></td>
                                </tr>
                                <tr>
                                    <td><strong>当前状态：</strong></td>
                                    <td>
                                        {% if plan.status == 'draft' %}
                                            <span class="badge bg-secondary">{{ plan.get_status_display }}</span>
                                        {% elif plan.status == 'active' %}
                                            <span class="badge bg-primary">{{ plan.get_status_display }}</span>
                                        {% elif plan.status == 'completed' %}
                                            <span class="badge bg-success">{{ plan.get_status_display }}</span>
                                        {% else %}
                                            <span class="badge bg-danger">{{ plan.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>创建人：</strong></td>
                                    <td>{{ plan.creator.get_full_name|default:plan.creator.username }}</td>
                                </tr>
                                <tr>
                                    <td><strong>创建时间：</strong></td>
                                    <td>{{ plan.created_at|date:"Y-m-d H:i" }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>计划时间：</strong></td>
                                    <td>{{ plan.planned_start_date|date:"Y-m-d H:i" }} ~ {{ plan.planned_end_date|date:"Y-m-d H:i" }}</td>
                                </tr>
                                {% if plan.actual_start_date %}
                                <tr>
                                    <td><strong>实际时间：</strong></td>
                                    <td>
                                        {{ plan.actual_start_date|date:"Y-m-d H:i" }}
                                        {% if plan.actual_end_date %} ~ {{ plan.actual_end_date|date:"Y-m-d H:i" }}{% endif %}
                                    </td>
                                </tr>
                                {% endif %}
                                <tr>
                                    <td><strong>盘点人员：</strong></td>
                                    <td>
                                        {% for assignee in plan.assignees.all %}
                                            <span class="badge bg-light text-dark me-1">{{ assignee.get_full_name|default:assignee.username }}</span>
                                        {% empty %}
                                            <span class="text-muted">未分配</span>
                                        {% endfor %}
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    {% if plan.description %}
                        <div class="mt-3">
                            <strong>计划描述：</strong>
                            <p class="mt-2">{{ plan.description }}</p>
                        </div>
                    {% endif %}
                </div>
            </div>
            
            <!-- 统计信息 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card text-center">
                        <div class="card-body">
                            <h3 class="text-primary">{{ stats.total }}</h3>
                            <p class="card-text">总资产数</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card text-center">
                        <div class="card-body">
                            <h3 class="text-success">{{ stats.completed }}</h3>
                            <p class="card-text">已完成</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card text-center">
                        <div class="card-body">
                            <h3 class="text-warning">{{ stats.discrepancies }}</h3>
                            <p class="card-text">发现差异</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card text-center">
                        <div class="card-body">
                            <h3 class="text-info">{{ stats.progress }}%</h3>
                            <p class="card-text">完成进度</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 盘点项目列表 -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-list me-2"></i>盘点项目</h6>
                </div>
                
                <div class="card-body">
                    <!-- 筛选器 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-4">
                            <select class="form-select" name="status">
                                <option value="">全部状态</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if value == selected_status %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-4">
                            <select class="form-select" name="discrepancy">
                                <option value="">全部项目</option>
                                <option value="yes" {% if selected_discrepancy == 'yes' %}selected{% endif %}>有差异</option>
                                <option value="no" {% if selected_discrepancy == 'no' %}selected{% endif %}>无差异</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-outline-primary">
                                <i class="fas fa-filter me-1"></i>筛选
                            </button>
                        </div>
                    </form>

                    <!-- 项目列表 -->
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>资产信息</th>
                                        <th>系统信息</th>
                                        <th>盘点状态</th>
                                        <th>盘点结果</th>
                                        <th>差异情况</th>
                                        <th>盘点人员</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for item in page_obj %}
                                        <tr {% if item.has_discrepancy %}class="table-warning"{% endif %}>
                                            <td>
                                                <strong>{{ item.asset.name }}</strong><br>
                                                <small class="text-muted">{{ item.asset.asset_number }}</small><br>
                                                <small class="text-muted">{{ item.asset.category.name }}</small>
                                            </td>
                                            <td>
                                                <small>
                                                    状态：{{ item.get_system_status_display }}<br>
                                                    {% if item.system_location %}位置：{{ item.system_location.name }}<br>{% endif %}
                                                    {% if item.system_user %}使用人：{{ item.system_user.get_full_name|default:item.system_user.username }}<br>{% endif %}
                                                    {% if item.system_department %}部门：{{ item.system_department.name }}{% endif %}
                                                </small>
                                            </td>
                                            <td>
                                                {% if item.status == 'pending' %}
                                                    <span class="badge bg-secondary">{{ item.get_status_display }}</span>
                                                {% elif item.status == 'in_progress' %}
                                                    <span class="badge bg-primary">{{ item.get_status_display }}</span>
                                                {% elif item.status == 'completed' %}
                                                    <span class="badge bg-success">{{ item.get_status_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-danger">{{ item.get_status_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if item.status == 'completed' %}
                                                    <small>
                                                        {% if item.physical_status %}状态：{{ item.get_physical_status_display }}<br>{% endif %}
                                                        {% if item.physical_location %}位置：{{ item.physical_location.name }}<br>{% endif %}
                                                        {% if item.physical_user %}使用人：{{ item.physical_user.get_full_name|default:item.physical_user.username }}<br>{% endif %}
                                                        {% if item.physical_department %}部门：{{ item.physical_department.name }}{% endif %}
                                                    </small>
                                                {% else %}
                                                    <span class="text-muted">-</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if item.has_discrepancy %}
                                                    <span class="badge bg-warning">{{ item.get_discrepancy_type_display }}</span>
                                                    {% if item.discrepancy_notes %}
                                                        <br><small class="text-muted">{{ item.discrepancy_notes|truncatechars:30 }}</small>
                                                    {% endif %}
                                                {% else %}
                                                    <span class="badge bg-success">无差异</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if item.checked_by %}
                                                    {{ item.checked_by.get_full_name|default:item.checked_by.username }}<br>
                                                    <small class="text-muted">{{ item.checked_at|date:"m-d H:i" }}</small>
                                                {% elif item.assigned_to %}
                                                    <span class="text-muted">分配给：{{ item.assigned_to.get_full_name|default:item.assigned_to.username }}</span>
                                                {% else %}
                                                    <span class="text-muted">未分配</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if plan.status == 'active' and item.status != 'completed' %}
                                                    <a href="{% url 'assets:inventory_item_check' item.pk %}" 
                                                       class="btn btn-outline-primary btn-sm">
                                                        <i class="fas fa-check me-1"></i>盘点
                                                    </a>
                                                {% elif item.status == 'completed' %}
                                                    <a href="{% url 'assets:inventory_item_check' item.pk %}" 
                                                       class="btn btn-outline-info btn-sm">
                                                        <i class="fas fa-eye me-1"></i>查看
                                                    </a>
                                                {% else %}
                                                    <span class="text-muted">-</span>
                                                {% endif %}
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="盘点项目分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_discrepancy %}&discrepancy={{ selected_discrepancy }}{% endif %}">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_discrepancy %}&discrepancy={{ selected_discrepancy }}{% endif %}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_discrepancy %}&discrepancy={{ selected_discrepancy }}{% endif %}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_discrepancy %}&discrepancy={{ selected_discrepancy }}{% endif %}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-list fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无盘点项目</h5>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
